#include "Symbol.h"
#include <string.h>
#include <assert.h>
#include <gc/gc.h>
int symbolcmp(const Symbol * first, const Symbol * second){
	/*Numbers first :-)
	And this should be deterministic!*/
	if(first->type != second->type){
		return first->type - second->type;
	}

	switch (first->type){
		case NUMBER:
			return numbercmp(first->number, second->number);
			break;
		case FUNCTION:
			/*this should work*/
			return (first->func - second->func);
			break;
		case OPERATOR:
			return (first->oper - second->oper);
			break;
		case VARIABLE:
		case CONSTANT:
			return strcmp(first->name, second->name);
			break;
	}
	/*We should not get here.*/
	assert(0);
	return 0;
}

void symbolcpy(Symbol * dest, const Symbol * src){
	if (src == dest){
		return;
	}
	dest->type=src->type;
	dest->func=src->func;
	dest->oper=src->oper;
	if(src->number != NULL){
		if(dest->number == NULL){
			dest->number = construct_number();
		}
		numbercpy(dest->number, src->number);
	}

	if(src->type == VARIABLE || src->type==CONSTANT){ 
		size_t length = strlen(src->name);
		GC_FREE(dest->name);
		dest->name=(char*) GC_MALLOC((length+1)*sizeof(char));
		strcpy(dest->name, src->name);
	}
}
